package com.netease.cloud.cm.retrofit.service;

import com.netease.cloud.cm.api.AdminUserService;
import com.netease.cloud.cm.api.AlarmService;
import com.netease.cloud.cm.api.ServerVersionService;
import com.netease.cloud.cm.retrofit.logger.OkHttpLogger;
import com.netease.cloud.cm.util.CommonStringUtils;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

public class RetrofitManager {

	private static final Retrofit retrofit = new Retrofit.Builder()
			.baseUrl(CommonStringUtils.getBaseUrl())
			.addConverterFactory(JacksonConverterFactory.create())
			.client(getOkHttpClient())
			.build();

	private static final Retrofit alarmRetrofit = new Retrofit.Builder()
			.baseUrl(CommonStringUtils.getAlarmUrl())
			.client(getOkHttpClient())
			.build();

	public static AdminUserService getAdminUserService() {
		return retrofit.create(AdminUserService.class);
	}

	public static ServerVersionService getServerVersionService() {
		return retrofit.create(ServerVersionService.class);
	}

	public static AlarmService getAlarmService() {
		return alarmRetrofit.create(AlarmService.class);
	}

	private static OkHttpClient getOkHttpClient() {
		OkHttpClient.Builder builder = new OkHttpClient.Builder();

		// 日志默认只输出到console，可以设置日志类型：NONE，BASIC, HEADERS,BODY
		// HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
		// httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);

		// 自定义拦截器OkHttpLogger将日志输出到文本
		builder.interceptors().add(new OkHttpLogger());
		return builder.build();
	}
}